package com.mayuhao.userstudent.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mayuhao.userstudent.entity.UserStudent;
import com.mayuhao.userstudent.mapper.UserStudentMapper;
import com.mayuhao.userstudent.service.IUserStudentService;
import com.mayuhao.userstudent.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 马宇豪
 * @since 2022-10-27
 */
@Slf4j
@Service
public class UserStudentServiceImpl extends ServiceImpl<UserStudentMapper, UserStudent> implements IUserStudentService {
    @Resource
    private UserStudentMapper studentMapper;

    /**
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.mayuhao.userstudent.entity.UserStudent>
     * @description 请修改方法描述
     * @methodName findPage
     * @params [pageNum, pageSize, searchId, searchName]
     * @paramType [java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/10/27 0027 下午 1:29
     * @email 1757442521@qq.com
     **/
    @Override
    public Page<UserStudent> findPage(Integer pageNum, Integer pageSize, String searchId, String searchName) {
        return studentMapper.page(new Page<>(pageNum, pageSize), searchId, searchName);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName chooseCourse
     * @params []
     * @paramType []
     * @author 马宇豪
     * @date 2022/10/27 0027 下午 4:16
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean chooseCourse(String courseId) {
        return studentMapper.chooseCourse(courseId);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName returnCourse
     * @params [courseId]
     * @paramType [java.lang.Integer]
     * @author 马宇豪
     * @date 2022/10/27 0027 下午 5:34
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean returnCourse(String courseId) {
        return studentMapper.returnCourse(courseId);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName saveChooseCourse
     * @params [studentId, courseId]
     * @paramType [java.lang.Integer, java.lang.Integer]
     * @author 马宇豪
     * @date 2022/10/27 0027 下午 5:35
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean saveChooseCourse(String studentId, String courseId) {
        return studentMapper.saveChooseCourse(studentId, courseId);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName deleteChooseCourse
     * @params [studentId, courseId]
     * @paramType [java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/10/30 0030 下午 8:40
     * @email 1757442521@qq.com
     **/
    @Override
    public boolean deleteChooseCourse(String studentId, String courseId) {
        return studentMapper.deleteChooseCourse(studentId, courseId);
    }

    /**
     * @return com.baomidou.mybatisplus.extension.plugins.pagination.Page<com.mayuhao.userstudent.entity.UserStudent>
     * @description 请修改方法描述
     * @methodName hasChoosePage
     * @params [pageNum, pageSize, searchId, searchName]
     * @paramType [java.lang.Integer, java.lang.Integer, java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/10/31 0031 上午 8:44
     * @email 1757442521@qq.com
     **/
    @Override
    public Page<UserStudent> hasChoosePage(Integer pageNum, Integer pageSize, String studentId, String searchId, String searchName) {
        return studentMapper.hasChoosePage(new Page<>(pageNum, pageSize), studentId, searchId, searchName);
    }

    /**
     * @return boolean
     * @description 请修改方法描述
     * @methodName selectBeforeChoose
     * @params [studentId, courseId]
     * @paramType [java.lang.String, java.lang.String]
     * @author 马宇豪
     * @date 2022/11/10 0010 上午 12:42
     * @email 1757442521@qq.com
     **/
    @Override
    public Result selectBeforeChoose(String studentId, String courseId, String courseName, String goCourseTime) {
        //可选课程数量
        int chooseCount = 9999;
        boolean flag = true;
        //1.查是否达到选课次数
        List<UserStudent> howManyChoose = studentMapper.howManyChoose(studentId);
        if (howManyChoose.size() < chooseCount) {

            //2.查是否此课你已经选择
            List<UserStudent> list = studentMapper.selectBeforeChoose(studentId, courseId);
            for (UserStudent userStudent : list) {
//                log.error(userStudent.getCourseName());
                if (courseName.equals(userStudent.getCourseName())) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                UserStudent userStudent = studentMapper.isFullChoose(courseId);


                List<UserStudent> findGoCourseTime = studentMapper.selectBeforeChooseFindGoCourseTime(studentId);
                String timeString = "";
                for (UserStudent student : findGoCourseTime) {
                    timeString += student.getGoCourseTime();
                }

                String[] split = goCourseTime.split("\\|");
                for (String s : split) {
                    if (timeString.contains(s)) {
                        return new Result(false, "你选择的课程与你已选课程的上课时间冲突！不可选！", 666);
                    }
                }

//                System.err.println(userStudent.getCourseChooseNumber());
//                System.err.println(userStudent.getClassSeatNumber());
                if (userStudent.getCourseChooseNumber() < userStudent.getClassSeatNumber()) {
                    return new Result(true, "ok", 200);
                } else {
                    return new Result(false, "已满 不可选", 200);
                }
            } else {
                return new Result(false, "重复！", 200);
            }
        } else {
            return new Result(false, "你已经选择9999课 不可再选！", 200);
        }
    }

    /**
     * @return java.util.List<com.mayuhao.userstudent.entity.UserStudent>
     * @description 请修改方法描述
     * @methodName scheduleSelect
     * @params []
     * @paramType []
     * @author 马宇豪
     * @date 2022/11/13 0013 下午 4:51
     * @email 1757442521@qq.com
     **/
    @Override
    public List<UserStudent> scheduleSelect() {
        return studentMapper.scheduleSelect();
    }
}
